<?php
/*
This is data modeller class communicating with 
databaseClass
*/

include('classDB/database_class.php');

class appModel extends database
{
	//var $dbInfo;
	
	
	function __construct()
	{
		parent::__construct(DB_NAME, DB_HOST, DB_USER, DB_PASSWORD);
		
		//unsetting validation session
		unset($_SESSION['v']);
	}
	
	public function getError()
	{
		return $this -> getMysqlError();
	}
	
	/*
	This functnio getting Single column
	*/
	
	function _getSingleData($pTableName, $pColName, $pCriteria = "1 = 1", $debug = false)
	{
		$sql = 	"SELECT ".$pColName."
				 FROM `".$pTableName."`
				 WHERE ".$pCriteria."";
				
		$this -> setQuery($sql, 'getSingleData');
		
		if($debug) 
			$this->printQuery();
				
		$this -> executeQuery();
		$row = $this -> fetch_array(1);
		//print_r($row);
		
		return $row[$pColName];
	}
	
	/*
	This function is getting Record Array
	*/
	public function _getArrayData($pTableName, $pColsName, $pCriteria = "1 = 1", $debug = false)
	{
		$sql = 	"SELECT ".trim($pColsName)."
				 FROM `".$pTableName."`
				 WHERE ".$pCriteria."";
				
		$this -> setQuery($sql, 'getData from Table '.$pTableName);
		
		if($debug) 
			$this -> printQuery();
				
		$this -> executeQuery();
		
		$tempArray = array();
		while ($row = $this -> fetch_array()) {
        	$tempArray[] = $row;
		}
		
		return $tempArray;
	}
	
	
	/*
	This function is getting Record Row
	*/
	public function _getRowData($pTableName, $pColsName, $pCriteria = "1 = 1", $debug = false)
	{
		$sql = 	"SELECT ".trim($pColsName)."
				 FROM `".$pTableName."`
				 WHERE ".$pCriteria."";
				
		$this -> setQuery($sql, 'getData from Table '.$pTableName);
		
		if($debug) 
			$this -> printQuery();
				
		$this -> executeQuery();
		
		$row = $this -> fetch_array();
        
		return $row;
	}
	
	
		
	
	
	/*
	======================= DB injection function [insert, update, deletee] =====================
	*/
	public function _insertTable($pTable, $arrData, $debug = false)		
	{	
		$sql = $this -> qmInsert($pTable, $arrData);

		$this -> setQuery($sql, 'insertTable');
			
		if($debug) 
			$this -> printQuery();
				
		if($this -> executeQuery())
			return true;
		else
			return false;
	
	}
	
	/*
	Delete record
	*/
	
	public function _XTable($pTable, $arrData, $debug = false)
	{
		$sql = $this -> qmDelete($pTable, $arrData);
	
		$this -> setQuery(htmlentities($sql), 'XTable '.$pTable);
			
		if($debug) 
			$this -> printQuery();
				
		if($this -> executeQuery())
			return true;
		else
			return false;
	
	}
	
	public function _updateTable($pTable, $pData, $pKey, $debug = false)	
	{
		//echo '<br />update Table '.$pValue.'<br />';
		$sql = $this -> qmUpdate($pTable, $pData, $pKey);
	
		$this -> setQuery($sql, 'updateTable');
			
		if($debug) 
			$this -> printQuery();
				
		if($this -> executeQuery())
			return true;
		else
			return false;
	
	}
	
	/*
	Prints query
	*/
	public function printQuery()
	{
		echo '<br> The query is being called from '.$this->queryFiredFrom.'<br>';
		echo 'Query = '.$this->query.'<br>';
		
		if($this->exitScript) 
		{
			echo '------- Exiting the script --------';
			exit;
		}
			
	}
	
	
	/*
	query makers
	*/
	public function qmInsert($pTable, $pData)
	{
		$sql = "";

		$sql = "INSERT INTO ".$pTable." (";
	
		foreach($pData as $field => $value):
			$sql .= trim($field).",";
		endforeach;
		
		$sql = substr($sql, 0, -1);			//CLIPPING THE LAST ,
	
		$sql .= ") VALUES (";
		
		foreach($pData as $field => $value):
			$sql .= CN_S($value) . ",";
		endforeach;
		
		$sql = substr($sql, 0, -1);			//CLIPPING THE LAST ,
		
		$sql .= ")";
		
	
		return $sql;
	}
	
	
	function qmDelete($pTable, $pData)
	{

		$sql = "";
		
		$tmpField=explode(";",$pField);
		$tmpValue=explode(";",$pValue);
		
		$sql = "DELETE FROM `".$pTable."` WHERE ";
		
		foreach($pData as $field => $value):
			$sql .= trim($field)." = ".CN_S($value)." AND ";
		endforeach;
		
		 /*for($i=0; $i<count($tmpField); $i++){
		
			$sql .= trim($tmpField[$i])."` = ".$tmpValue[$i]." AND `";
		
		}*/
		
		$sql = substr($sql, 0, -5);			//CLIPPING THE LAST ,
		
		return $sql;
		
	}
	
	function qmUpdate($pTable, $pData, $pKey)
	{
		$sql = "";

		$sql = "UPDATE `".$pTable."` SET `";
		
		foreach($pData as $field => $value):
			$sql .= trim($field)."` = ".CN_S($value).",`";
		endforeach;
		
		$sql = substr($sql, 0, -2);			//CLIPPING THE LAST ,
		$sql .= " WHERE `";
		
		foreach($pKey as $field => $value):
			$sql .= trim($field)."` = ".CN_S($value)." AND `";
		endforeach;
		
		$sql = substr($sql, 0, -5);			//CLIPPING THE LAST ' AND '
		
		return $sql;	
	}
	
	/*
		This function checks for duplicate entry in database, returns True/False.
	*/
	public function isAlreadyExist($table, $field, $value)
	{
	
		$duplicateRec = $this-> _getSingleData($table, 'COUNT(*)', "$field = '$value'");
		if($duplicateRec > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}	


	
}
?>